package searchtab.search.options;

import java.util.HashSet;
import java.util.Iterator;

public class SearchOptions {

    public static final String HIGHLIGHT_ALL = "highlightAll";

    public static final String MATCH_CASE = "matchCase";
    
    private static SearchOptions instance = new SearchOptions();

    private boolean matchCase;

    private boolean highlightAll;

    private HashSet<ISearchOptionsListener> listeners = new HashSet<ISearchOptionsListener>();

    public SearchOptions() {
    }

    public static boolean isMatchCase() {
        return instance.matchCase;
    }

    public static void setMatchCase(boolean matchCase) {
        instance.fireEvent(new SearchOptionsChangedEvent(MATCH_CASE, matchCase, instance.matchCase));
        instance.matchCase = matchCase;
    }

    private void fireEvent(SearchOptionsChangedEvent event) {
        Iterator<ISearchOptionsListener> iter = listeners.iterator();
        while (iter.hasNext()) {
            iter.next().onSearchOptionChanged(event);
        }
    }

    public static boolean isHighlightAll() {
        return instance.highlightAll;
    }

    public static void setHighlightAll(boolean highlightAll) {
        instance.fireEvent(new SearchOptionsChangedEvent(HIGHLIGHT_ALL, highlightAll, instance.highlightAll));
        instance.highlightAll = highlightAll;
    }

    public void addListener(ISearchOptionsListener listener) {
        listeners.add(listener);
    }

    public void removeListener(ISearchOptionsListener listener) {
        listeners.remove(listener);
    }

    public void removeAllListeners() {
        listeners.clear();
    }

    public static SearchOptions reset() {
        return instance = new SearchOptions();
    }

    public static SearchOptions getInstance() {
        return instance;
    }

}
